<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0px;padding: 0px;
        }
        #box{
            width: 512px;
            height: 400px;
            border: 1px solid #f00;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
        }
        span{
            position: absolute;height: 200px;width: 100%;left: 0px;
        }
        span#goUp{
            top: 0px;
        }
        span#goDown{
            bottom: 0px;
        }
        #pic{
            position: absolute;top: 0px;left: 0px;
        }

    </style>
</head>
<body>
    <div id="box">
        <img src="img/mi.png" id="pic">
        <span id="goUp"></span>
        <span id="goDown"></span>
    </div>
    <script type="text/javascript">
        // 封装函数
        function getID(id) {
            return document.getElementById(id);
        }

        // var goUp = document.getElementById("goUp");
        // var goDown = document.getElementById("goDown");
        // var pic = document.getElementById("pic");
        var num = 0;
        var timer = null;
        getID("goUp").onmouseover = function () {
            clearInterval(timer);//防止在向上移动的过程中，多次鼠标移入，导致速度越来越快
            timer = setInterval(function () {
                num = num-3;
                num>=-1070?pic.style.top=num+"px":clearInterval(timer);
            },30)
        }
        getID("goDown").onmouseover = function () {
            clearInterval(timer);//防止在向上移动的过程中，多次鼠标移入，导致速度越来越快
            timer = setInterval(function () {
                num = num+3;
                num<0?getID("pic").style.top=num+"px":clearInterval(timer);
            },30)
        }
        getID("box").onmouseout = function () {
            clearInterval(timer);
        }

    </script>

</body>
</html>